//
//  MYVisitorLoginView.swift
//  MYSinaWeicoe//
//  Created by Magic Yu on 16/4/7.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol MYVisitorLoginViewDelegate: NSObjectProtocol {

    //声明方法
    //默认不加@objc 但是如果要使用optional 必须加@objc
    optional func userWillLogin()
    optional func userWillRegister()
    
}

class MYVisitorLoginView: UIView {
    
    //声明代理对象 要注意循环引用
    weak var visitorDelegate: MYVisitorLoginViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //提供一个对外的方法 设置页面显示内容
    func visitorSetInfo(tipText: String,imgName: String,isHome: Bool = false) {
    
        if isHome {
            //如果是首页 执行动画
            startAnimation()
        }else {
            //如果不是首页 隐藏大房子
            houseImg.hidden = true
            //将图片移到最上层
            bringSubviewToFront(circleImg)
        }
        tipLabel.text = tipText
        circleImg.image = UIImage(named: imgName)
        
    }
    
    //动画
    private func startAnimation() {
    
        //开启基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画
        animation.duration = 15
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        //设置旋转角度
        animation.toValue = 2 * M_PI
        //设置动画不移除图层
        animation.removedOnCompletion = false

        //添加动画(动画添加到图层上)
        circleImg.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //加载UI控件
    private func setUI(){
    
      //SnapKit的使用 必须先添加进视图 再设置约束 否则会报错
        addSubview(circleImg)
        addSubview(backImg)
        addSubview(houseImg)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //VFL设置约束
        
        /**
        *  获取一个约束对象
        *
        *  @param item          需要设置约束的视图
        *  @param attribute     需要约束的属性
        *  @param relatedBy     约束优先级 一般都是精准约束.equal
        *  @param toItem        约束参考的对象
        *  @param attribute     参考约束的属性
        *  @param multiplier    乘积系数 一般是1  不能是0
        *  @param constant      约束值
        *
        *  @return 约束对象
        */
        
        //现将autoResizing取消,关闭frame布局
        //要给哪个控件设置约束 就将哪个控件的frame布局取消
//        self.translatesAutoresizingMaskIntoConstraints = false
//        circleImg.translatesAutoresizingMaskIntoConstraints = false
//        self.addConstraint(NSLayoutConstraint(item: circleImg, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        
        //使用SnapKit设置约束
        
        circleImg.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        backImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImg)
        }
        
        houseImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImg)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleImg.snp_bottom).offset(16)
            //为了能够自动换行 需要约束宽度
            make.width.equalTo(230)
            make.centerX.equalTo(circleImg)
            //约束高度 解决按钮跳转的问题
            make.height.equalTo(40)
        }
        
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        self.backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)
        
//        startAnimation()
        
        //按钮添加点击事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        
    }
    
    @objc private func loginBtnClick() {
    
        print("登录登录")
        //使用代理对象 调用代理方法
        //1.判断代理对象是否为nil
        //2.判断代理对象是否响应代理方法
        visitorDelegate?.userWillLogin?()
    }
    
    @objc private func registerBtnClick() {
    
        print("注册注册")
        visitorDelegate?.userWillRegister?()
    }
    
    
    //把子控件懒加载出来
    //背景图片
    private lazy var backImg:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圆圈背景图片
    private lazy var circleImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //房子图片
    private lazy var houseImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示label
    private lazy var tipLabel: UILabel = {
    
        let l = UILabel()
        
        l.text      = "关注一些人,看看这里有什么惊喜关注一些人,看看这里有什么惊喜"
        l.textColor = UIColor.grayColor()
        l.font      = UIFont.systemFontOfSize(14)
        //设置最大显示宽度
//        l.preferredMaxLayoutWidth = 230
        l.numberOfLines = 0
        //设置居中
        l.textAlignment = .Center
        
        return l
        
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
    
        let btn  = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")
        let capW = Int((image?.size.width)! * 0.5)
        let capH = Int((image?.size.height)! * 0.5)
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn
        
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
    
        let register = UIButton()
        
        register.setTitle("注册", forState: .Normal)
        register.titleLabel?.font = UIFont.systemFontOfSize(14)
        register.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return register
        
    }()
    
    

}
